--[[--
- @Copyright (C), 2016-, sixiong.
- @Name common.lua
- @Author sixiong
- @Version 1.0
- @Date: 2016年6月11日下午22:06:41
- @Description 常用函数
- @Class
- @Function List
- @History <author> <time> <version > <desc>
    sixiong 2016年6月11日下午22:06:41  1.0  第一次建立该文件
--]] 

--[[
- @desc   lua数据输出
- @param  string   字符串 
- return  string
--]]
function dump(v)
	if not __dump then
		function __dump(v, t, p)    
			local k = p or "";

			if type(v) ~= "table" then
				table.insert(t, k .. " : " .. tostring(v));
			else
				for key, value in pairs(v) do
					__dump(value, t, k .. "[" .. key .. "]");
				end
			end
		end
	end

	local t = {'======== Lib:Dump Content ========'};
	__dump(v, t);
	print(table.concat(t, "\n"));
end


--[[
- @desc   字串分割
- @param  string   字符串
- @param  string   关键字符串
- return  table
--]]
function split(s, d)
	if not s  or (string.len(s) <= 0) then return {} end;
	local t = {};
	local p = "(.-)" .. d;
	local r = 0;

	while r <= string.len(s) do
		local start, last, part = string.find(s, p, r);
		if not start then
			table.insert(t, string.sub(s, r));
			r = string.len(s) + 1;
		else
			if #part > 0 then table.insert(t, part) end;
			r = last + 1;
		end
	end 
	return t;
end

 
--[[
- @desc   解析URL参数
- @param  string   url路径
- return  table
--]]
function urlparam(s)
	if string.len(s or '') <= 0 then return {} end; 
	local t = {};
	local finder = split(s, '&'); 
	
	for _, p in ipairs(finder) do
		local _1, _2, k, v = string.find(p, "([%w_]+)=(.+)");
		if k and v then t[k] = v end;
	end 
	return t;
end


 


--[[
- @desc   判断文件是否存在 
- @param  string   f文件路径
- return  boole
--]]
function file_exists(f) 
	   local s = io.open(f, 'rb');
	   if not s  then 
	      return false
	   end; 
       return io.close(s); 
end 
 
 
-- 序列化一个table 
function  serialize(obj)
    local lua = ""  
    local t = type(obj)  
    if t == "number" then  
        lua = lua .. obj  
    elseif t == "boolean" then  
        lua = lua .. tostring(obj)  
    elseif t == "string" then  
        lua = lua .. string.format("%q", obj)  
    elseif t == "table" then  
        lua = lua .. "{\n"  
    for k, v in pairs(obj) do  
        lua = lua .. "[" .. serialize(k) .. "]=" .. serialize(v) .. ",\n"  
    end  
    local metatable = getmetatable(obj)  
        if metatable ~= nil and type(metatable.__index) == "table" then  
        for k, v in pairs(metatable.__index) do  
            lua = lua .. "[" .. serialize(k) .. "]=" .. serialize(v) .. ",\n"  
        end  
    end  
        lua = lua .. "}"  
    elseif t == "nil" then  
        return nil  
    else  
        error("can not serialize a " .. t .. " type.")  
    end  
    return lua  
end


-- 反序列化一个table
function  unserialize(lua)
    local t = type(lua)  
    if t == "nil" or lua == "" then  
        return nil  
    elseif t == "number" or t == "string" or t == "boolean" then  
        lua = tostring(lua)  
    else  
        error("can not unserialize a " .. t .. " type.")  
    end  
    lua = "return " .. lua  
    local func = loadstring(lua)  
    if func == nil then  
        return nil  
    end  
    return func() 
end
 
 
--[[
- @desc   传入一个table 返回一个新的table,并将传入的table,作为新table的一个属性,新table 有被设置了元表，元方法信息  
- @param  table   base   
- return  table
--]]
function inherit(base)
	local t = { super = base }
	setmetatable(t, { __index = function(t, k) return t.super[k] end })
	return t;
end
 
 
--[[
- @desc   取出一个vendor
- @param  string   vendor文件
- return  table
--]]
function vendor(class)
  --return dofile( _PATH..'core/vendor/' ..class ..'.lua'); 
	return require('core.vendor.' .. class);
end
 
